home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / tablewidth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-27  |  4.1 KB  |  157 lines

  1. /*
  2.     TableWidth XFCN v1.1
  3.     
  4.     ©1991 Apple Computer, Inc.; by Mike Byrne
  5.     
  6.     This is a kinda weird XFCN, but very cool.  It takes a string of any length, and returns a string
  7.     of the width specified.  If the given string is too short, spaces are added.  If the string is
  8.     too long, it is truncated.  In addition, the XFCN checks to make sure that the last character
  9.     is a space, so that the table entries are all pretty...
  10.     
  11.     Form:
  12.     TableWidth(<string>, <width>)
  13.     
  14.     # the MPW 3.2 build commands:
  15.     C -b TableWidth.c -mbg off
  16.         Link -w -t STAK -c WILD -rt XFCN=613 ∂
  17.             -m ENTRYPOINT ∂
  18.             -sg TableWidth ∂
  19.             TableWidth.c.o ∂
  20.             "{Libraries}HyperXLib.o" ∂
  21.             "{Libraries}Runtime.o" ∂
  22.             "{Libraries}Interface.o" ∂
  23.             "{CLibraries}StdCLib.o" ∂
  24.             -o "teststack"
  25. */
  26.  
  27. #include <Types.h>
  28. #include <Packages.h>
  29. #include <string.h>
  30. #include <Memory.h>
  31. #include "HyperXCmd.h"
  32.  
  33. #define NULL  '\0'
  34. #define NIL 0L
  35.  
  36. #define kNumParams 2
  37.  
  38.  
  39. /* prototypes */
  40. void ErrorBack(XCmdPtr paramPtr, char *message);
  41. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  42. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  43.  
  44.  
  45.  
  46. pascal void EntryPoint(XCmdPtr paramPtr)
  47. {
  48.     /* variable declarations */
  49.     long    width;
  50.     char    strWidth[101];
  51.     char    retString[101];
  52.     long    giveLen;
  53.     
  54.  
  55.     /* move high and lock the parameters. */
  56.     MoveLockParams(paramPtr, paramPtr->paramCount);
  57.  
  58.     /* check for copyright or syntax help request */
  59.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  60.         ErrorBack(paramPtr, "v1.1, ©1991 Apple Computer, Inc.; by Mike Byrne");
  61.         UnlockParams(paramPtr, paramPtr->paramCount);
  62.         return;
  63.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  64.         ErrorBack(paramPtr, "TableWidth syntax is 'TableWidth(<string>, <width>)");
  65.         UnlockParams(paramPtr, paramPtr->paramCount);
  66.         return;
  67.     }
  68.  
  69.     /* not a copyright or help request.       */     
  70.     /* check for correct number of parameters */
  71.     if (paramPtr->paramCount != kNumParams) {
  72.         ErrorBack(paramPtr, "Error: TableWidth syntax is 'TableWidth(<string>, <width>)'");
  73.         UnlockParams(paramPtr, paramPtr->paramCount);
  74.         return;
  75.     }
  76.  
  77.     /* figure out the width of the column. */
  78.     strcpy(strWidth, (char*)*paramPtr->params[1]);
  79.     c2pstr(strWidth);
  80.     StringToNum(strWidth, &width);
  81.     if ( (width < 1) || (width > 100) ) {
  82.         ErrorBack(paramPtr, "Error: The width is too low or too high.");
  83.         UnlockParams(paramPtr, paramPtr->paramCount);
  84.         return;
  85.     }
  86.  
  87.     /* check to see if we're wider than the spec.  if so, truncate... */
  88.     giveLen = strlen( (char*)*paramPtr->params[0] );
  89.     if ( giveLen >= width) {
  90.         strncpy(retString, (char*)*paramPtr->params[0], width);
  91.         retString[width] = NULL;
  92.         if (retString[width-1] != ' ') { retString[width-1] = ' '; }
  93.         ErrorBack(paramPtr,retString);
  94.         UnlockParams(paramPtr, paramPtr->paramCount);
  95.         return;
  96.     }
  97.  
  98.     /* otherwise, copy the requisite number of spaces onto the end. */        
  99.     p2cstr(strWidth);
  100.     strcpy(strWidth,"                                                                                                         ");
  101.     strcpy(retString, (char*)*paramPtr->params[0]);
  102.     strncat(retString, strWidth, (width-giveLen));
  103.     
  104.     ErrorBack(paramPtr,retString);
  105.     UnlockParams(paramPtr, paramPtr->paramCount);
  106.     return;
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.     
  114. /* allocate and load up paramPtr->returnValue with a string 
  115.    -------------------------------------------------------- */
  116. void ErrorBack(XCmdPtr paramPtr, char *message)
  117. {
  118.     Handle  mesHnd;
  119.  
  120.     /*
  121.         Allocate space for an error message.
  122.         Copy the string into it.
  123.         Return the handle to HyperCard.
  124.     */
  125.     mesHnd = NewHandle((long)(strlen(message)+1));
  126.     if (mesHnd == nil) return;
  127.     strcpy((char *)*mesHnd,message);
  128.     paramPtr->returnValue = mesHnd;
  129. }
  130.  
  131.  
  132.  
  133. /*  move high and lock down all parameters  
  134.     ----------------------------------------------------------------------- */
  135. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  136. {
  137.     short i;
  138.     
  139.     for(i=0; i <= paramCount-1; i++)
  140.     {
  141.         MoveHHi(paramPtr->params[i]);
  142.         HLock(paramPtr->params[i]);
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /* unlock all parameter handles in the XCmdBlock  
  150.    ---------------------------------------------  */
  151. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  152. {    short i;
  153.     
  154.     for(i=0; i <= paramCount-1; i++)
  155.         { HUnlock(paramPtr->params[i]);}
  156. }
  157.